package com.kzd.gtboon.custom

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.style.ClickableSpan
import android.view.View
import android.webkit.WebView
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.kzd.gtboon.AgreeActivity
import com.kzd.gtboon.R
import com.kzd.gtboon.main.*
import com.kzd.gtboon.main.mine.MineRichTextActivity
import com.kzd.gtboon.main.popup.LoadingDialog
import com.module.lemlin.http.*
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import java.util.*

fun BaseQuickAdapter<*, BaseViewHolder>.setEmptyView() {
    setEmptyView(
        createViewEmptyNull(context, "暂无数据", R.color.white)
    )
}

fun Activity.setTransitionStatus(response: HttpResponseBody<*>) {
    when (response.status) {
        Status.START -> {
            LoadingDialog.show(this as Context, R.layout.dialog_load_style_1)
        }
        Status.FAILURE -> {
            LoadingDialog.dismiss()
            ToastUtils.showShort(response.error?.msg)
        }
        Status.SUCCESS -> {
            LoadingDialog.dismiss()
        }
    }
}

fun Fragment.setTransitionStatus(response: HttpResponseBody<*>) {
    when (response.status) {
        Status.START -> {
            LoadingDialog.show(requireContext(), R.layout.dialog_load_style_1)
        }
        Status.FAILURE -> {
            LoadingDialog.dismiss()
            ToastUtils.showShort(response.error?.msg)
        }
        Status.SUCCESS -> {
            LoadingDialog.dismiss()
        }
    }
}

fun MultiStatusLayout.setTransitionStatus(response: HttpResponseBody<*>) {
    when (if (isStatusSuccess) return else response.status) {
        Status.START -> {
            showLoadingView()
        }
        Status.FAILURE -> {
            showLoadingHint(hint = response.error?.msg)
        }
        Status.SUCCESS -> {
            hideLoadingView()
            isStatusSuccess = true
        }
    }
}


fun <E> Activity.setTransitionStatusE(
    response: HttpResponseBody<HttpResponseDataE<E>>,
    adapter: BaseQuickAdapter<*, BaseViewHolder>? = null,
    refresh: SmartRefreshLayout? = null, isEnableLoadMore: Boolean = true,
) {
    setTransitionStatusE(this as Context, response, adapter, refresh, isEnableLoadMore)
}

fun <E> Fragment.setTransitionStatusE(
    response: HttpResponseBody<HttpResponseDataE<E>>,
    adapter: BaseQuickAdapter<*, BaseViewHolder>? = null,
    refresh: SmartRefreshLayout? = null, isEnableLoadMore: Boolean = true,
) {
    setTransitionStatusE(requireContext(), response, adapter, refresh, isEnableLoadMore)
}

private fun <E> setTransitionStatusE(
    context: Context, response: HttpResponseBody<HttpResponseDataE<E>>,
    adapter: BaseQuickAdapter<*, BaseViewHolder>? = null,
    refresh: SmartRefreshLayout? = null, isEnableLoadMore: Boolean,
) {
    val data = response.data?.data
    when (response.status) {
        Status.START -> {
            if (data.isNullOrEmpty() && adapter?.data.isNullOrEmpty() && adapter?.hasEmptyView() == false) {
                adapter.setEmptyView(
                    createViewEmptyLoad(context, color = R.color.white)
                )
                refresh?.setEnableRefresh(false)
                refresh?.setEnableLoadMore(false)
            }
        }
        Status.FAILURE -> {
            val error = response.error?.msg
            if (data.isNullOrEmpty() && adapter?.data.isNullOrEmpty()) {
                adapter?.setEmptyView(
                    createViewEmptyNull(context, error, R.color.white)
                )
                refresh?.setEnableLoadMore(false)
            } else {
                ToastUtils.showShort(error)
                refresh?.setEnableLoadMore(isEnableLoadMore)
            }
            refresh?.finishRefresh((false))
            refresh?.finishLoadMore((false))
            refresh?.setEnableRefresh(true)
        }
        Status.SUCCESS -> {
            if (data.isNullOrEmpty() && adapter?.data.isNullOrEmpty()) {
                adapter?.setEmptyView(
                    createViewEmptyNull(context, "暂无数据", R.color.white)
                )
                refresh?.setEnableLoadMore(false)
            } else {
                adapter?.removeEmptyView()
                refresh?.setEnableLoadMore(isEnableLoadMore)
            }
            refresh?.finishRefresh((true))
            refresh?.finishLoadMore((true))
            refresh?.setEnableRefresh(true)
        }
    }
}


fun <T> Activity.setTransitionStatusT(
    response: HttpResponseBody<HttpResponseDataT<T>>,
    adapter: BaseQuickAdapter<*, BaseViewHolder>? = null,
    refresh: SmartRefreshLayout? = null, isEnableLoadMore: Boolean = true,
) {
    setTransitionStatusT(this as Context, response, adapter, refresh, isEnableLoadMore)
}

fun <T> Fragment.setTransitionStatusT(
    response: HttpResponseBody<HttpResponseDataT<T>>,
    adapter: BaseQuickAdapter<*, BaseViewHolder>? = null,
    refresh: SmartRefreshLayout? = null, isEnableLoadMore: Boolean = true,
) {
    setTransitionStatusT(requireContext(), response, adapter, refresh, isEnableLoadMore)
}

private fun <T> setTransitionStatusT(
    context: Context, response: HttpResponseBody<HttpResponseDataT<T>>,
    adapter: BaseQuickAdapter<*, BaseViewHolder>? = null,
    refresh: SmartRefreshLayout? = null, isEnableLoadMore: Boolean,
) {
    val data = response.data?.data
    when (response.status) {
        Status.START -> {
            if (data == null && adapter?.data.isNullOrEmpty() && adapter?.hasEmptyView() == false) {
                adapter.setEmptyView(
                    createViewEmptyLoad(context, color = R.color.white)
                )
                refresh?.setEnableRefresh(false)
                refresh?.setEnableLoadMore(false)
            }
        }
        Status.FAILURE -> {
            val error = response.error?.msg
            if (data == null && adapter?.data.isNullOrEmpty()) {
                adapter?.setEmptyView(
                    createViewEmptyNull(context, error, R.color.white)
                )
                refresh?.setEnableLoadMore(false)
            } else {
                ToastUtils.showShort(error)
                refresh?.setEnableLoadMore(isEnableLoadMore)
            }
            refresh?.finishRefresh((false))
            refresh?.finishLoadMore((false))
            refresh?.setEnableRefresh(true)
        }
        Status.SUCCESS -> {
            if (data == null && adapter?.data.isNullOrEmpty()) {
                adapter?.setEmptyView(
                    createViewEmptyNull(context, "暂无数据", R.color.white)
                )
                refresh?.setEnableLoadMore(false)
            } else {
                adapter?.removeEmptyView()
                refresh?.setEnableLoadMore(isEnableLoadMore)
            }
            refresh?.finishRefresh((true))
            refresh?.finishLoadMore((true))
            refresh?.setEnableRefresh(true)
        }
    }
}


fun <K, V> Activity.setTransitionStatusM(
    response: HttpResponseBody<HttpResponseDataM<K, V>>,
    adapter: BaseQuickAdapter<*, BaseViewHolder>? = null,
    refresh: SmartRefreshLayout? = null, isEnableLoadMore: Boolean = true,
) {
    setTransitionStatusM(this as Context, response, adapter, refresh, isEnableLoadMore)
}

fun <K, V> Fragment.setTransitionStatusM(
    response: HttpResponseBody<HttpResponseDataM<K, V>>,
    adapter: BaseQuickAdapter<*, BaseViewHolder>? = null,
    refresh: SmartRefreshLayout? = null, isEnableLoadMore: Boolean = true,
) {
    setTransitionStatusM(requireContext(), response, adapter, refresh, isEnableLoadMore)
}

private fun <K, V> setTransitionStatusM(
    context: Context, response: HttpResponseBody<HttpResponseDataM<K, V>>,
    adapter: BaseQuickAdapter<*, BaseViewHolder>? = null,
    refresh: SmartRefreshLayout? = null, isEnableLoadMore: Boolean,
) {
    val data = response.data?.data
    when (response.status) {
        Status.START -> {
            if (data.isNullOrEmpty() && adapter?.data.isNullOrEmpty() && adapter?.hasEmptyView() == false) {
                adapter.setEmptyView(
                    createViewEmptyLoad(context, color = R.color.white)
                )
                refresh?.setEnableRefresh(false)
                refresh?.setEnableLoadMore(false)
            }
        }
        Status.FAILURE -> {
            val error = response.error?.msg
            if (data.isNullOrEmpty() && adapter?.data.isNullOrEmpty()) {
                adapter?.setEmptyView(
                    createViewEmptyNull(context, error, R.color.white)
                )
                refresh?.setEnableLoadMore(false)
            } else {
                ToastUtils.showShort(error)
                refresh?.setEnableLoadMore(isEnableLoadMore)
            }
            refresh?.finishRefresh((false))
            refresh?.finishLoadMore((false))
            refresh?.setEnableRefresh(true)
        }
        Status.SUCCESS -> {
            if (data.isNullOrEmpty() && adapter?.data.isNullOrEmpty()) {
                adapter?.setEmptyView(
                    createViewEmptyNull(context, "暂无数据", R.color.white)
                )
                refresh?.setEnableLoadMore(false)
            } else {
                adapter?.removeEmptyView()
                refresh?.setEnableLoadMore(isEnableLoadMore)
            }
            refresh?.finishRefresh((true))
            refresh?.finishLoadMore((true))
            refresh?.setEnableRefresh(true)
        }
    }
}

/**
 * 点击防抖监听器操作
 * */
fun View.setOnClickAntiShakeListener(delayTime: Long = 300, onClick: (view: View) -> Unit) {
    var lastClickTime: Long = 0
    setOnClickListener {
        val curTime = Calendar.getInstance().timeInMillis
        if (curTime - lastClickTime < delayTime) {
            return@setOnClickListener
        }
        lastClickTime = curTime
        if (it == null) {
            return@setOnClickListener
        }
        onClick(it)
    }
}

fun String.toAgreeLinks(activity: Activity): SpannableString {
    val spannable = SpannableString(this)
    spannable.setSpan(
        object : ClickableSpan() {
            override fun onClick(widget: View) {
//                toMineRichTextActivityApi(
//                    RichTextApi.AGREEMENT_YHXY.title,
//                    RichTextApi.AGREEMENT_YHXY.type
//                )
                val intent = Intent(activity, MineRichTextActivity::class.java)
                val bundle = Bundle().apply {
                    putString(KEY_RICH_TEXT_TITLE, RichTextApi.AGREEMENT_YHXY.title)
                    putString(KEY_RICH_TEXT_TYPE, RichTextApi.AGREEMENT_YHXY.type)
                }
                intent.putExtras(bundle)
                activity.startActivity(intent)
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = ColorUtils.getColor(R.color.colorMain)
                ds.isUnderlineText = false
            }
        },
        this.indexOf(string = "《"),
        this.indexOf(string = "》") + 1,
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
    )
    spannable.setSpan(
        object : ClickableSpan() {
            override fun onClick(widget: View) {
//                toMineRichTextActivityApi(
//                    RichTextApi.AGREEMENT_YSZC.title,
//                    RichTextApi.AGREEMENT_YSZC.type
//                )
                val intent = Intent(activity, MineRichTextActivity::class.java)
                val bundle = Bundle().apply {
                    putString(KEY_RICH_TEXT_TITLE, RichTextApi.AGREEMENT_YSZC.title)
                    putString(KEY_RICH_TEXT_TYPE, RichTextApi.AGREEMENT_YSZC.type)
                }
                intent.putExtras(bundle)
                activity.startActivity(intent)
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = ColorUtils.getColor(R.color.colorMain)
                ds.isUnderlineText = false
            }
        },
        this.lastIndexOf(string = "《"),
        this.lastIndexOf(string = "》") + 1,
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
    )
    return spannable
}
